//
//  XiaoBianMoreTableViewController.m
//  瓜子电台
//
//  Created by lanou on 16/8/5.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "XiaoBianMoreTableViewController.h"
#import "XiaoBianMoreModel.h"
#import "XiaoBianMoreTableViewCell.h"
#import "TrackListDetailViewController.h"

@interface XiaoBianMoreTableViewController ()

@property (nonatomic, strong) NSMutableArray *modelArray;

@property (nonatomic, assign) NSInteger pageId;

@property (nonatomic, assign) NSInteger maxPageId;

@end

@implementation XiaoBianMoreTableViewController

- (NSMutableArray *)modelArray
{
    if (!_modelArray) {
        self.modelArray = [[NSMutableArray alloc] init];
    }
    return _modelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"小编推荐";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"XiaoBianMoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_moreXiaoBian"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self requestData];
    [self headerAndFooter];
}

- (void)requestData
{
    if (self.pageId == 0) {
        self.pageId = 1;
    }
    NSString *url = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iPhone&pageId=1&pageSize=20&statEvent=pageview%2Falbumlist%40%E5%B0%8F%E7%BC%96%E6%8E%A8%E8%8D%90&statModule=%E5%B0%8F%E7%BC%96%E6%8E%A8%E8%8D%90_%E6%9B%B4%E5%A4%9A&statPage=tab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&statPosition=1";
    NSString *url1 = [url stringByReplacingOccurrencesOfString:@"pageId=1" withString:[NSString stringWithFormat:@"pageId=%ld", self.pageId]];
    [NetWorkrequestManage requestWithType:GET url:url1 parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        self.maxPageId = [dic[@"maxPageId"] integerValue];
        for (NSDictionary *dataDic in dic[@"list"]) {
            XiaoBianMoreModel *model = [[XiaoBianMoreModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
            if (self.pageId == self.maxPageId) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        });
        
    } error:^(NSError *error) {
        
    }];
}

- (void)headerAndFooter
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageId = 1;
        [self.modelArray removeAllObjects];
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageId++;
        [self requestData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XiaoBianMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_moreXiaoBian" forIndexPath:indexPath];
    XiaoBianMoreModel *model = self.modelArray[indexPath.row];
    [cell setDataWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 105;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TrackListDetailViewController *vc = [[TrackListDetailViewController alloc] init];
    XiaoBianMoreModel *model = self.modelArray[indexPath.row];
    NSLog(@"%ld", model.albumId);
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
